package com.zy.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class LineSetView extends View {

    //系数
    float factor = 1;

    public LineSetView(Context context) {
        super(context);
        init(context);
    }


    public LineSetView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LineSetView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public LineSetView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    Paint paint;

    private void init(Context context) {
        paint = new Paint();
        paint.setColor(Color.RED);
    }

    public void setFactor(float factor) {
        this.factor = factor;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //Log.d("LineSetView",""+getWidth()+" "+getHeight());
        int height = getHeight();
        for (LineNode lineNode : lineNodeList) {
            paint.setColor(lineNode.color);
            canvas.drawRect(lineNode.startX, height + lineNode.startY, lineNode.stopX, height + lineNode.stopY, paint);
        }

        if (quick) {
            paint.setColor(Color.RED);
            int y = height - pivot;
            canvas.drawLine(0, y, getWidth(), y, paint);
        }
    }

    List<LineNode> lineNodeList = new ArrayList<>();

    //100
    //200
    //400
    //800
    public void setData(int[] dataS, List<Integer> opIndex, List<Integer> sortedIndex) {
        lineNodeList.clear();

        int width = getWidth();
        float diWidth = (float) width / (float) dataS.length;

        float di = diWidth * 5 / 6;
        float emptyDi = diWidth / 6;


        float startX = 0;

        for (int i = 0; i < dataS.length; i++) {
            int data = dataS[i];

            float stopX = startX + di;

            int color;
            if (opIndex.contains(i)) {
                color = Color.RED;
            } else {
                color = baseColor;
            }

            if (sortedIndex.contains(i)) {
                color = Color.GREEN;
            }


            lineNodeList.add(new LineNode(startX, -data * factor, stopX, 0, color));
            startX = stopX + emptyDi;
        }
        invalidate();
    }

    boolean quick = false;
    int pivot = 0;

    public void setQuickData(int pivot) {
        quick = true;
        this.pivot = pivot;
    }

    public void setQuickEnable(boolean enable) {
        quick = enable;
    }


    public void ending(int[] dataS, int endingIndex) {
        lineNodeList.clear();

        int width = getWidth();
        float diWidth = (float) width / (float) dataS.length;

        float di = diWidth * 5 / 6;
        float emptyDi = diWidth / 6;


        float startX = 0;
        for (int i = 0; i < dataS.length; i++) {
            int data = dataS[i];
            float stopX = startX + di;
            int color;
            if (i < endingIndex) {
                color = Color.GREEN;
            } else {
                color = baseColor;
            }

            if (endingIndex == dataS.length - 1) {
                color = Color.GREEN;
            }

            lineNodeList.add(new LineNode(startX, -data * factor, stopX, 0, color));
            startX = stopX + emptyDi;
        }
        invalidate();
    }

    static int baseColor = Color.WHITE;

    public static class LineNode {
        float startX;
        float startY;
        float stopX;
        float stopY;

        int color = baseColor;

        public LineNode(float startX, float startY, float stopX, float stopY, int color) {
            this.startX = startX;
            this.startY = startY;
            this.stopX = stopX;
            this.stopY = stopY;
            this.color = color;
        }
    }
}
